package cn.com.xyc.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;

public class MD5 {
	private static String[] intParam = { "0", "1", "2", "3", "4", "5", "6",
			"7", "8", "9" };
	private static String[] strParam = { "a", "b", "c", "d", "e", "f", "g",
			"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
			"u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
			"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
			"U", "V", "W", "X", "Y", "Z" };

	public static Map<String, String> encode(String enc1, String enc2) {
		String salt = new SecureRandomNumberGenerator().nextBytes().toHex();
		int hashIterations = 2;
		SimpleHash hash = new SimpleHash("md5", enc1, enc2 + salt,
				hashIterations);
		Map<String, String> values = new HashMap<String, String>();
		values.put("pwd", hash.toHex());
		values.put("salt", salt);
		values.put("code", makeActiveCode(32));
		return values;
	}

	/**
	 * 生成 激活码
	 * 
	 */
	public static String makeActiveCode(int num) {
		String code1 = "";
		int int2 = strParam.length;
		Random ra = new Random();
		for (int i = 0; i < num; i++) {
			code1 = code1 + intParam[ra.nextInt(10)];
			code1 = code1 + strParam[ra.nextInt(int2)];
		}
		StringBuffer buf = new StringBuffer(code1);
		StringBuffer newCode = new StringBuffer();
		for (int i = code1.length(); i > 0; i--) {
			int randomInt = ra.nextInt(buf.length());
			newCode.append(buf.charAt(randomInt));
			buf.deleteCharAt(randomInt);
		}
		return newCode.toString();
	}
}
